/**
 * Created with Vim7.3 ubuntu12.04
 * @fileOverview : 
 * @author : Lyle <lylechen2014@gmial.com>
 * @since : 2015-01-22 09:51:15
 * @filename : Gruntfile.js
 * @version : 
 * @description : 自动化构建学习 
 */

module.exports = function (grunt) {
 
  //初始化
  grunt.initConfig({
    //读入项目配置信息
    pkg: grunt.file.readJSON('package.json'),

    //合并插件
    concat: {
      options: {
         //定义一个用于插入合并输出文件之间的字符
         separator: ';'
      },
      dist: {
        //合并前的JS文件的存放位置,注意有些JS具有依赖顺序压缩
        //src: ['src/zepto.js', 'src/touch.js', 'src/event.js'],
        src: 'src/zepto/*.js',
        //合并后的JS文件的存放位置
        //dest: 'dist/<%= pkg.name %>.js'
        dest: 'dist/zepto/zepto.js'
      }
    },
   
    //压缩 
    uglify: {
      options: {
         //此处定义的banner注释将插入到输出文件的顶部
         //banner: '/*! <%= pkg.name %> <%= grunt.template.today("dd-mm-yyyy") %> */\n'
      },
      my_target: {
         files: {
            'dist/zepto/zepto.min.js': ['dist/zepto/zepto.js']
         }
      }
    },

    //CSS压缩合并
    cssmin: {
      target: {
        files: {
          //注意CSS具有先后顺序，覆盖的问题。压缩合并必须数组方式
          'dist/resource/css/demo.min.css': ['src/resource/css/public.css', 'src/resource/css/index.css']
        }
      }
    },

    //图片压缩
    imagemin: {      
        dynamic: {
            options: {
                //优化级别1-7，默认是3，效果不明显                        
                optimizationLevel: 3,  
            },
            files: [{
                expand: true,
                cwd: "src/resource/img/",
                src: ['*.{png,jpg,gif}'],
                dest: "dist/resource/img/"
            }]
        }
     },

    //watch插件的配置信息,监听测试不是每次修改都监听的到？还需要在测试
    /*
    watch: {

      scripts: { 
        files: ['src/zepto/*.js'], 
        tasks: ['default'], 
      },
          
      src: {
        files: ['src/zepto/*.js','src/resource/css/*.css','src/resource/img/*.*'],
        tasks: ['default'],
      }
      
    }
    */
  });
  // 加载提供任务的插件
  grunt.loadNpmTasks('grunt-contrib-uglify');
  grunt.loadNpmTasks('grunt-contrib-concat');
  grunt.loadNpmTasks('grunt-contrib-cssmin');
  grunt.loadNpmTasks('grunt-contrib-imagemin');
  //grunt.loadNpmTasks('grunt-contrib-watch');
  //grunt.loadNpmTasks('grunt-newer');
  // 默认任务
  grunt.registerTask('default', ['concat','uglify','cssmin','imagemin']);
  //grunt.registerTask('default', ['concat','uglify','cssmin','imagemin','watch']);
  //grunt.registerTask('default', ['concat','uglify','cssmin','newer:imagemin','watch']);
}

